package com.bkgy.modular.tbdsjplancheckpdiC642.service;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.alibaba.fastjson.JSONObject;
import com.bkgy.excelhandle.PlanExcelHandlerImpl;
import com.bkgy.modular.qms.entity.QmsCurveDataMongo;
import com.bkgy.modular.tbdsjplancheckpdiC642.dao.PlanCheckPdi642Dao;
import com.bkgy.modular.tbdsjplancheckpdiC642.entity.ExportPlanCheckPdi642;
import com.bkgy.modular.tbdsjplancheckpdiC642.entity.PlanCheckPdi642;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.ExcelUtilsPOI;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PlanCheckPdi642Service {

    @Resource
    private PlanCheckPdi642Dao planCheckPdi642Dao;
    @Resource
    private MongoTemplate mongoTemplate;

    public ResultData findDataByPage(JSONObject json) {
        try {
            JSONObject condition = json.getJSONObject("condition");
            // (pageIndex - 1) * pageSize, pageIndex * pageSize,
            List<PlanCheckPdi642> list = planCheckPdi642Dao.findDataByPage(condition);
            Integer count = planCheckPdi642Dao.findDataCount(condition);
            return ResultData.ResultDataSuccess(list, count);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("查询失败,失败信息为" + e.getMessage(), null);
        }
    }

    /**
     * 列表导出
     * @param jsonObject
     * @param response
     */
    public void export(JSONObject jsonObject, HttpServletResponse response) {
        // 接收传入参数
        String fileNames = jsonObject.getString("fileName");
        JSONObject condition = jsonObject.getJSONObject("condition");

        // 创建表格数据集合
        List<PlanCheckPdi642> listExcel = planCheckPdi642Dao.findDataByPage(condition);
        // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
        /*List<PlanCheckPdi642> data = planCheckPdi642Dao.findDataByPage(condition);
        if (!data.isEmpty() && data.size() > 0) {
            for (PlanCheckPdi642 item : data) {
                listExcel.add(item);
            }
        }*/
        if (CollectionUtils.isEmpty(listExcel)) {
            return;
        }
        List<ExportPlanCheckPdi642> list = new ArrayList<>();
        for (PlanCheckPdi642 item : listExcel) {
            ExportPlanCheckPdi642 exportPlanCheckPdi642 = new ExportPlanCheckPdi642();
            BeanUtils.copyProperties(item,exportPlanCheckPdi642);
            list.add(exportPlanCheckPdi642);
        }
        try {
            Map<String,Object> data1 = new HashMap();
            data1.put("data",list);
            File file = ResourceUtils.getFile("classpath:exceltemplate\\2130连退生产计划审核.xlsx");
            TemplateExportParams templateExportParams = new TemplateExportParams(file.getPath(),false);
            templateExportParams.setDictHandler(new PlanExcelHandlerImpl());
            Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, data1);
            ExcelUtilsPOI.exportExcel(fileNames,response,workbook);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        //ExcelUtils.downloadExcel(fileNames,response, PlanCheckPdi642.class,listExcel);
    }

    public ResultData findLineDataByVolumeNo(JSONObject json) {
        String volumeNo = json.getString("volumeNo");
        if (!StringUtils.hasText(volumeNo)) {
            return ResultData.ResultDataFaultSelf(null, "传入卷号为空");
        }

        Map<String, Map<String, Object>> map = new HashMap<>();
        Query query1 = new Query(Criteria.where("volumeNo").is(volumeNo));
        QmsCurveDataMongo qmsCurveDataMongo = mongoTemplate.findOne(query1, QmsCurveDataMongo.class);

        Map<String, Object> mapValue = new HashMap<>();
        if(!ObjectUtils.isEmpty(qmsCurveDataMongo)){
            List<Double> list1 = qmsCurveDataMongo.getOriginBolg().get("hsm_fdh_thk_cold");
            List<Double> list2 = qmsCurveDataMongo.getOriginBolg().get("hsm_fdw_width_cold");
            List<Double> list3 = qmsCurveDataMongo.getOriginBolg().get("hsm_crown_c40");
            List<Double> list4 = qmsCurveDataMongo.getOriginBolg().get("hsm_flatness_wedge40");
            List<Double> list5 = qmsCurveDataMongo.getOriginBolg().get("hsm_fdt_fdt");
            List<Double> list6 = qmsCurveDataMongo.getOriginBolg().get("hsm_ctc_ct_temp");
            List<Double> list7 = qmsCurveDataMongo.getOriginBolg().get("hsm_flatness_symmetric_flatness");
            List<Double> list8 = qmsCurveDataMongo.getOriginBolg().get("hsm_flatness_asymmetric_flatness");

            mapValue.put("hsm_fdh_thk_cold", list1);
            mapValue.put("hsm_fdw_width_cold", list2);
            mapValue.put("hsm_crown_c40", list3);
            mapValue.put("hsm_flatness_wedge40", list4);
            mapValue.put("hsm_fdt_fdt", list5);
            mapValue.put("hsm_ctc_ct_temp", list6);
            mapValue.put("hsm_flatness_symmetric_flatness", list7);
            mapValue.put("hsm_flatness_asymmetric_flatness", list8);
        }

        map.put(volumeNo, mapValue);
        if (!CollectionUtils.isEmpty(map)) {
            return ResultData.ResultDataSuccess(map);
        }
        return ResultData.ResultDataFaultSelf(null, "查询数据为空");
    }
}
